//
//  WCNMyPatientVC.m
//  WCN
//
//  Created by wsj2015 on 15/9/21.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNMyPatientVC.h"
#import "WCNetworkAccessManager.h"
#import "WCPopupLayerManager.h"
#import "Account.h"
#import "WCDefaultManager.h"
#import "Macros.h"
#import "WCNItemChooseView.h"
#import "MJRefresh.h"

#import "WCNFansModel.h"
#import "WCNUserCell.h"
#import "WCNFansCell.h"
#import "WCNPatientBtn.h"
#import "WCNTypePatientVC.h"

#import "WCNOrderDetailVC.h"
#import "WCCoreDataStore.h"

#import "WCNFansDetailVC.h"

@interface WCNMyPatientVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _selectItem;
    
    NSString *_nurseId;
   
    NSInteger _pageForOrder;
    NSInteger _pageForFans;
    NSInteger _totalPageForOrder;
    NSInteger _totalpageForFans;
    
    
    
    NSMutableArray *_orderUserArray;
    NSMutableArray *_fansArray;

}


@property (weak, nonatomic) IBOutlet WCNPatientBtn *orderPatientAction;

@property (weak, nonatomic) IBOutlet WCNPatientBtn *fansBtn;
@property (weak, nonatomic) IBOutlet WCNPatientBtn *patienInHospitalBtn;




@property (weak, nonatomic) IBOutlet WCNItemChooseView *chooseView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation WCNMyPatientVC

- (IBAction)switchOrderPatientAction:(id)sender {
   
    self.tableView.alpha =0;

    [self.orderPatientAction setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.orderPatientAction.backgroundColor =[UIColor orangeColor];
    [self.fansBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.fansBtn.backgroundColor =[UIColor whiteColor];
    _selectItem =0;
    [self.tableView.header beginRefreshing];
}

- (IBAction)switchFansAction:(id)sender {

    [self.fansBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.fansBtn.backgroundColor =[UIColor orangeColor];
    [self.orderPatientAction setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.orderPatientAction.backgroundColor =[UIColor whiteColor];
    _selectItem =1;
    [self getFansWithIsReCover:YES];
    self.tableView.alpha =0;
}

- (IBAction)swithInHosPitalAction:(id)sender {
    
    WCNTypePatientVC *vc =[[WCNTypePatientVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(instancetype)init
{
    if (self =[super init]) {
        self.title =@"我的患者";
    }
    return self;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithParms];
    [self configUI];
}


- (void)initWithParms
{
    _pageForOrder =1;
    _pageForFans =1;
    _selectItem =0;
    _orderUserArray =[NSMutableArray array];
    _fansArray =[NSMutableArray array];
    

    
    
   
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    Account *account = [[WCDefaultManager shareMg] account];
    _nurseId =account.stringId;

    [UIView animateWithDuration:0.3f animations:^{
        
        self.tableView.frame =CGRectMake(0, 64, kScreenWidth,kScreenHeight -180);
        
        
    }];

}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIView animateWithDuration:0.3f animations:^{
        
        self.tableView.frame =CGRectMake(-kScreenWidth, 64, kScreenWidth,kScreenHeight -180);
        
        
    }];

}
- (void)configUI
{
    
    self.title =@"我的患者";
    [self.orderPatientAction setRoundCorner];
    [self.fansBtn setRoundCorner];
    [self.patienInHospitalBtn setRoundCorner];
    
    
    weakSelf_define;

    //选择视图
    self.chooseView.clickBlock = ^(NSInteger selctedIndex){
        _selectItem =selctedIndex;
        [weakSelf.tableView.header beginRefreshing];
    };
    
    
    [self.tableView registerNib:[UINib nibWithNibName:nibNameForOrder bundle:nil] forCellReuseIdentifier:OrderPathientReusedId];
    [self.tableView registerNib:[UINib nibWithNibName:kFansNibName bundle:nil] forCellReuseIdentifier:kFansReuseId];
       //添加上拉 和下拉
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        if (_selectItem == 0) {
            _pageForOrder =1;
            [weakSelf getOrderPatientArrayIsRecover:YES];
        }else{
            _pageForFans =1;
            [weakSelf getFansWithIsReCover:YES];
        }
        
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        
        if (_selectItem ==0) {
            _pageForOrder ++;
            [weakSelf getOrderPatientArrayIsRecover:NO];
        }else{
            _pageForFans ++;
            [weakSelf getFansWithIsReCover:NO];
        }
        
    }];

    //第一次刷新一次
    [self.tableView.header beginRefreshing];

}

//"nurseId": 15,
//"category": 1,
//"page": 1,
//"rows": 2
- (void)getOrderPatientArrayIsRecover:(BOOL)isRecover
{
    
    MBProgressHUD *hud =showHud;
    weakSelf_define;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"nurseId"] =_nurseId;
    params[@"category"] =@1;
    params[@"page"] =@(_pageForOrder);
    params[@"rows"] =@8;
    
    [[WCNetworkAccessManager sharedMg]POST:kOrderPatient parameters:params success:^(NSDictionary *responseObject) {
      
        
        NSDictionary *dictData =responseObject[@"data"];
        if ( _totalPageForOrder) {
            if (_pageForOrder == _totalPageForOrder) {
                showTextOnlyForlastPage;
            }
        }
        _totalPageForOrder  =[dictData[@"totalPage"] integerValue];
    
        if (isRecover) {
            [_orderUserArray removeAllObjects];
        
        }

        for (NSDictionary *dict in dictData[@"result"]) {
            Order *order =[[Order alloc] init];
            order.nursingOrder_Status =[[NursingOrderStatus alloc]init];
            [order.nursingOrder_Status setValuesForKeysWithDictionary:dict[@"nursingOrderStatus"]];


            order.service_Type =[[ServiceType alloc] init];
            [order.service_Type setValuesForKeysWithDictionary:dict[@"serviceType"]];

            [order setValuesForKeysWithDictionary:dict];
            [_orderUserArray addObject:order];
        }
        [weakSelf loadNOrecodView:weakSelf.tableView dataArray:_orderUserArray];

        [UIView animateWithDuration:0.3f animations:^{
            weakSelf.tableView.alpha =1;
        }];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSString *msg) {
      
    } netError:nil  always:^{
        [hud hide:YES];
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
    }];
    
    
 

}
- (void)getFansWithIsReCover:(BOOL)isCover
{
    MBProgressHUD *hud =showHud;
    weakSelf_define;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"nurseAccountId"] =_nurseId;
//    params[@"category"] =@1;
    params[@"page"] =@(_pageForFans);
    params[@"rows"] =@8;
    [[WCNetworkAccessManager sharedMg] POST:kFans_part parameters:params success:^(NSDictionary  *responseObject) {
        if (isCover) {
            [_fansArray removeAllObjects];
        }
        NSDictionary *dataDict =responseObject[@"data"];
        for (NSDictionary *fansDict in dataDict[@"result"]) {
            WCNFansModel *fans =[[WCNFansModel alloc]init];
            [fans setValuesForKeysWithDictionary:fansDict];
            [_fansArray addObject:fans];
        }
        [weakSelf loadNOrecodView:weakSelf.tableView dataArray:_fansArray];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSString *msg) {
        
    } netError:nil always:^{
        [hud hide:YES];
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        [UIView animateWithDuration:0.3f animations:^{
            weakSelf.tableView.alpha =1;
        }];
    }];




}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num =0;
    if (_selectItem == 0) {
        num =_orderUserArray.count;
    }else{
    
        num =_fansArray.count;
    }
    
    return num;
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if (_selectItem == 0) {
        cell =[self tableViewForOrderPatient:tableView indexPath:indexPath];
    }else{
        cell =[self tableViewForFans:tableView indexPath:indexPath];
    }
    return cell;
}
- (WCNUserCell *)tableViewForOrderPatient:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    WCNUserCell *cell =[tableView dequeueReusableCellWithIdentifier:OrderPathientReusedId forIndexPath:indexPath];
    Order *model =_orderUserArray[indexPath.row];
 
   
    [cell configModel:model];
    return cell;
 
}
- (WCNFansCell *)tableViewForFans:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    WCNFansCell *cell =[tableView dequeueReusableCellWithIdentifier:kFansReuseId forIndexPath:indexPath];
    WCNFansModel *model =_fansArray[indexPath.row];
    
    [cell configFansModel:model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_selectItem == 0) {
      
        if (_orderUserArray.count >0) {
            [self jumpToOrderDetailVCWith:_orderUserArray[indexPath.row]];

        }
            }else{
        WCNFansModel *fans =  _fansArray[indexPath.row];
        WCNFansDetailVC *vc =[[WCNFansDetailVC alloc]init];
        vc.patienId =[fans.wcId integerValue];
        [self.navigationController pushViewController:vc animated:YES];
    }

}
- (void)jumpToOrderDetailVCWith:(Order *)model
{
    WCNOrderDetailVC *vc =[[WCNOrderDetailVC alloc]init];
    vc.orderId = model.orderId;
    [self.navigationController pushViewController:vc animated:YES];

   

}



@end
